package com.ad.pet.app.account.order;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.account.order.OrderDetail;

import javax.inject.Inject;

/**
 * Created by SEELE on 2019/2/18.
 */
@PerActivity
public class PacketDetailPresenter extends AbsBasePresenter implements PacketDetailContract.Presenter{
    private ApiService mApiService;
    private PacketDetailContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;
    @Inject
    public PacketDetailPresenter(ApiService apiService,
                                Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }
    @Override
    public void onCreate(PacketDetailContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void getPacketDetail(String pgId) {
        mRxDisposable.add(mApiService.package_detail(pgId).subscribe(new AppSubscriber<OrderDetail>(mView) {
            @Override
            public void next(OrderDetail detail) {
                mView.getPacketDetailSuccess(detail);
            }
        }));
    }

    @Override
    public void returnBanlacne(String order_sn, String banlanceSn) {
        mRxDisposable.add(mApiService.return_balance(order_sn,banlanceSn).subscribe(new AppSubscriber<User>(mView) {
            @Override
            public void next(User detail) {
                mView.returnBanlanceSuccess();
            }
        }));
    }

    @Override
    public void changeAddress(String order_id, String firstname, String lastname, String address, String country_id, String state_id, String state, String city_id, String city, String postcode, String phone) {
        mRxDisposable.add(mApiService.change_order_detail_address(order_id,firstname, lastname,
                address, country_id, state_id,state,city_id,city,
                postcode, phone)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.changeAddressSuccesss();
                    }
                }));
    }
}
